package com.dfbz.controller.view;


import com.dfbz.controller.BaseServlet;
import com.dfbz.model.Article;
import com.dfbz.model.Conference;
import com.dfbz.model.Dept;
import com.dfbz.service.ArticleService;
import com.dfbz.service.DeptService;
import com.dfbz.service.MeetService;
import com.dfbz.service.UserService;
import com.dfbz.service.impl.ArticleServiceImpl;
import com.dfbz.service.impl.DeptServiceImpl;
import com.dfbz.service.impl.MeetServiceImpl;
import com.dfbz.service.impl.UserServiceImpl;
import com.dfbz.utils.PageUtil;
import com.dfbz.utils.StrUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

@WebServlet("/view/*")
public class UIServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    private ArticleService articleService = new ArticleServiceImpl();

    private DeptService deptService = new DeptServiceImpl();

    private MeetService meetService = new MeetServiceImpl();

    public void index(HttpServletRequest req, HttpServletResponse resp){
        try {
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void forget(HttpServletRequest req, HttpServletResponse resp){
        try {
            req.getRequestDispatcher("/forget.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void home(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.getRequestDispatcher("/html/home.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void showRegister(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.getRequestDispatcher("/register.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public void userList(HttpServletRequest req, HttpServletResponse resp){
        try {
            String currentPage = req.getParameter("page");
            String search = req.getParameter("search");
            if(null != search && !StrUtil.isContainChinese(search)){
                search = URLDecoder.decode(search,"UTF-8");
                byte[] bytes = search.getBytes("ISO-8859-1");
                search = new String(bytes,"UTF-8");
            }
            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }
            String userId = req.getSession().getAttribute("userId").toString();
            List<Map<String, Object>> list = userService.findList(Integer.parseInt(userId), page, search);
            long totalPage = PageUtil.getTotalPage(userService.findTotalUserCount(search));
            long startPage = PageUtil.getStartPage(page) + 1;
            long endPage = PageUtil.getEndPage(page, totalPage);
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("currentPage", page);
            req.setAttribute("startPage", startPage);
            req.setAttribute("endPage", endPage);
            req.setAttribute("search", search);
            req.setAttribute("userList", list);
            req.getRequestDispatcher("/html/user.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void myUserList(HttpServletRequest req, HttpServletResponse resp){
        try {
            String ufId = req.getParameter("ufId");
            String currentPage = req.getParameter("page");
            String search = req.getParameter("search");
            if(null != search && !StrUtil.isContainChinese(search)){
                search = URLDecoder.decode(search,"UTF-8");
                byte[] bytes = search.getBytes("ISO-8859-1");
                search = new String(bytes,"UTF-8");
            }
            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }
            if (StrUtil.isNotEmpty(ufId)) {
                userService.unFollow(StrUtil.stringToInteger(ufId));
            }
            List<Map<String, Object>> list = userService.findList(Integer.parseInt(ufId), page, search);
            long totalPage = PageUtil.getTotalPage(userService.findTotalUserCount(search));
            long startPage = PageUtil.getStartPage(page) + 1;
            long endPage = PageUtil.getEndPage(page, totalPage);
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("currentPage", page);
            req.setAttribute("startPage", startPage);
            req.setAttribute("endPage", endPage);
            req.setAttribute("search", search);
            req.setAttribute("myUserList", list);
            req.getRequestDispatcher( "/html/my_user.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void userDetail(HttpServletRequest req, HttpServletResponse resp){
        try {
            String uId = req.getParameter("uId");
            Map<String,Object> detailInfo = userService.getDetail(Integer.parseInt(uId));
            req.setAttribute("detailInfo",detailInfo);
            req.getRequestDispatcher("/html/user_detail.jsp").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public void userLook(HttpServletRequest req, HttpServletResponse resp){
        try {
            String uId = req.getParameter("uId");
            Map<String,Object> detailInfo = userService.getDetail(Integer.parseInt(uId));
            req.setAttribute("detailInfo",detailInfo);
            List<Dept> deptList = deptService.findAll();
            req.setAttribute("deptList",deptList);
            req.getRequestDispatcher("/html/user_look.jsp").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void articleList(HttpServletRequest request, HttpServletResponse response) {
        try {
            String currentPage = request.getParameter("page");
            String search = request.getParameter("search");
            if (null != search && !StrUtil.isContainChinese(search)) {
                search = URLDecoder.decode(search, "UTF-8");
                byte[] bytes = search.getBytes("ISO-8859-1");
                search = new String(bytes, "UTF-8");
            }
            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }
            List<Article> list = articleService.findList();
            long totalPage = PageUtil.getTotalPage(articleService.findTotalCount(search));
            long startPage = PageUtil.getStartPage(page) + 1;
            long endPage = PageUtil.getEndPage(page, totalPage);
            request.setAttribute("totalPage", totalPage);
            request.setAttribute("currentPage", page);
            request.setAttribute("startPage", startPage);
            request.setAttribute("endPage", endPage);
            request.setAttribute("search", search);
            request.setAttribute("articleList", list);
            request.getRequestDispatcher("/html/article.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deptList(HttpServletRequest request, HttpServletResponse response){
        try {
            request.setAttribute("deptList",deptService.findDeptList());
            request.getRequestDispatcher("/html/department.jsp").forward(request, response);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public void meetList(HttpServletRequest request, HttpServletResponse response) {
        try {
            String currentPage = request.getParameter("page");
            String search = request.getParameter("search");
            String deptId = request.getParameter("deptId");
            if (null != search && !StrUtil.isContainChinese(search)) {
                search = URLDecoder.decode(search, "UTF-8");
                byte[] bytes = search.getBytes("ISO-8859-1");
                search = new String(bytes, "UTF-8");
            }
            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }
            List<Conference> list = meetService.findList(page,Integer.parseInt(deptId),search);
            request.setAttribute("meetList", list);
            long totalPage = PageUtil.getTotalPage(meetService.findTotalCount(Integer.parseInt(deptId),search));
            long startPage = PageUtil.getStartPage(page) + 1;
            long endPage = PageUtil.getEndPage(page, totalPage);
            request.setAttribute("startPage", startPage);
            request.setAttribute("endPage", endPage);
            request.setAttribute("search", search);
            request.setAttribute("totalPage", totalPage);
            request.setAttribute("currentPage", page);
            request.setAttribute("deptId", deptId);
            request.getRequestDispatcher("/html/meeting.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
